/*********************************************************************************
 *      Copyright:  (C) 2022 Zhang xiangyun<1327295677@qq.com>
 *                  All rights reserved.
 *
 *       Filename:  pack.c
 *    Description:  This file 
 *                 
 *        Version:  1.0.0(07/05/22)
 *         Author:  Zhang xiangyun <1327295677@qq.com>
 *      ChangeLog:  1, Release initial version on "07/05/22 14:38:47"
 *                 
 ********************************************************************************/
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <time.h>
#include <stdlib.h>

#include "pack.h"
#include "logger.h"
#include "ds18b20.h"
#include "pack.h"
#include "cJSON.h"
#include "get_conf.h"

int get_id(char *serial_id, int size, int sn);
int get_time(char *sample_time);
int cjson_pack(pack_info_t pack_info,char **p,int option);
int packet_data(pack_info_t pack_info,char *pack_buf,int size);

int get_id(char *serial_id, int size, int sn)
{
	if( !serial_id || size<DEVID_LEN )
	{
		log_error("get_id() Invalid input arugments\n");
		return -1;
	}
	memset(serial_id,0,size);
	snprintf(serial_id,size,"RPI#%04d",sn);
	return 0;
}

int get_time(char *sample_time)
{
	struct tm *ptm;
	time_t   now;
	if(!sample_time)
	{
		log_error("get_time() Invalid input arugments\n");
		return -1;
	}
	time(&now);
	ptm=localtime(&now);

	memset(sample_time,0,TIME_LEN);
	snprintf(sample_time,TIME_LEN,"%02i:%02i:%02i",ptm->tm_hour,ptm->tm_min,ptm->tm_sec);

	return 0;
}

int packet_data(pack_info_t pack_info,char *pack_buf,int size)
{

	memset(pack_buf,0,size);
	snprintf(pack_buf,size,"%s/%s/%s",pack_info.serial_id,pack_info.sample_time,pack_info.temper);
	return strlen(pack_buf);
}


int cjson_pack(pack_info_t pack_info,char **p,int option)
{
	cJSON                    *item=NULL;
	cJSON                    *Method=NULL;

	char                     *pack;
	float                     temper;

	temper=strtof(pack_info.temper,&pack);

	if(ALI==option)
	{
		

    	item=cJSON_CreateObject();
        if(NULL == item)
     	{
     		printf("create cjson error!\n");
     		return -1;
    	}
    	cJSON_AddStringToObject(item,"method","thing.service.property.set");
    	cJSON_AddStringToObject(item,"id","1046186801");
     	Method=cJSON_CreateObject();
     	if(NULL==Method)
     	{
     		printf("create method cjson error!\n");
    		return -1;
    	}
    	cJSON_AddStringToObject(Method,"data",pack_info.sample_time);
     	cJSON_AddNumberToObject(Method,"CurrentTemperature",temper);
    	cJSON_AddItemToObject(item,"params",Method);

	    cJSON_AddStringToObject(item,"version","1.0.0");

	//char *p=NULL;
    	*p=cJSON_Print(item);
    	if(NULL==*p)
    	{
    		cJSON_Delete(item);
    		return -1;
     	}

    	printf("p:%s\n",*p);
     	return 0;
	}

	if(TENCENT==option)
	{
		cJSON *item2=cJSON_CreateObject();
    	if(NULL==item2)
    	{
     		printf("create cjson1 error!\n");
     		return -1;
     	}
     	cJSON_AddStringToObject(item2,"method","report");
     	cJSON_AddStringToObject(item2,"clientToken","clientToken-fe51446d-bef7-4812-8f87-ebd5b1156a49");
    	cJSON *item3=cJSON_CreateObject();
     	if(NULL==item3)
    	{
    		printf("create cjson2 error!\n");
     		return -1;
    	}
     	cJSON_AddNumberToObject(item3,"lac",0);
     	cJSON_AddNumberToObject(item3,"cid",0);
    	cJSON_AddNumberToObject(item3,"CurrentTemperature",temper);
    	cJSON_AddStringToObject(item3,"time",pack_info.sample_time);
    	cJSON_AddItemToObject(item2,"params",item3);
	//printf("1111\n");
	//cJSON_AddItemToObject(item1,NULL,item2);

    	//char *p=NULL;
    	*p=cJSON_Print(item2);
     	if(NULL==*p)
     	{
     		cJSON_Delete(item2);
    		return -1;
     	}

    	printf("p:%s\n",*p);
		return 0;

	}

	if(HUAWEI==option)
	{
		cJSON *root=cJSON_CreateObject();
     	if(NULL==root)
    	{
     		printf("create cjson1 error!\n");
     		return-1;
     	}
     	cJSON *item1=cJSON_CreateArray();
    	if(NULL==item1)
     	{
    		printf("create cjson1 error!\n");
    		return -1;
     	}
    	cJSON *item2=cJSON_CreateObject();
     	if(NULL==item2)
    	{
      		printf("create cjson2 error!\n");
    		return -1;
    	}
     	cJSON_AddStringToObject(item2,"service_id","1");
     	cJSON *item3=cJSON_CreateObject();
     	if(NULL==item3)
    	{
     		printf("create cjson3 error!\n");
     		return -1;
     	}
     	cJSON_AddNumberToObject(item3,"CurrentTemperature",temper);
    	cJSON_AddStringToObject(item3,"time",pack_info.sample_time);
    	cJSON_AddItemToObject(item2,"properties",item3);
    	cJSON_AddItemToArray(item1,item2);
    	cJSON_AddItemToObject(root,"services",item1);


     	//char *p=NULL;
     	*p=cJSON_Print(root);
    	if(NULL==*p)
     	{
     		cJSON_Delete(root);
     		return -1;
     	}

    	printf("*p:%s\n",*p);
		//printf("this!!!!!\n");
		return 0;
  	}

}
